<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-card">
        <div class="login-header">
          <h2>博物馆导览系统</h2>
          <p class="subtitle">欢迎回来，请登录您的账号</p>
        </div>
        <el-form
            :model="loginForm"
            :rules="loginRules"
            ref="loginFormRef"
            class="login-form"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                :prefix-icon="User"
                placeholder="请输入用户名"
                class="material-input"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                :prefix-icon="Lock"
                type="password"
                placeholder="请输入密码"
                show-password
                class="material-input"
            />
          </el-form-item>
          <el-form-item prop="role">
            <el-select
                v-model="loginForm.role"
                placeholder="请选择角色"
                class="material-select"
            >
              <el-option label="管理员" value="ADMIN"/>
              <el-option label="游客" value="VISITOR"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
                type="primary"
                :loading="loading"
                @click="handleLogin"
                class="login-button"
                :class="{ 'button-loading': loading }"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        <div class="login-options">
          <el-button
              text
              @click="goToRegister"
              class="register-link"
          >
            注册账号
          </el-button>
          <el-button
              text
              @click="forgetPassword"
              class="forget-link"
          >
            忘记密码？
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, reactive} from 'vue'
import {useRouter, useRoute} from 'vue-router'
import {useStore} from 'vuex'
import {ElMessage} from 'element-plus'
import {User, Lock} from '@element-plus/icons-vue'
import {login} from '@/api/user'

const router = useRouter()
const route = useRoute()
const store = useStore()

const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  role: ''
})

const loginRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
  ],
  role: [
    {required: true, message: '请选择角色', trigger: 'change'}
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (!valid) return

    loading.value = true
    try {
      // 调用登录接口
      const response = await login(loginForm)
      console.log("response", response)
      // 保存用户信息和token
      const userInfo = {
        ...response,
        role: loginForm.role
      }
      await store.dispatch('login', userInfo)

      // 登录成功后的跳转
      const redirect = route.query.redirect
      if (loginForm.role === 'ADMIN') {
        router.replace(redirect || '/admin/dashboard')
        ElMessage.success('管理员登录成功')
      } else {
        router.replace(redirect || '/visitor/home')
        ElMessage.success('游客登录成功')
      }
    } catch (error) {
      ElMessage.error(error.response?.data?.message || '登录失败')
    } finally {
      loading.value = false
    }
  })
}

const goToRegister = () => {
  router.push('/register')
}

const forgetPassword = () => {
  ElMessage.info('请联系管理员重置密码')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('@/assets/images/museum.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-content {
  width: 100%;
  max-width: 440px;
  padding: 20px;
  perspective: 1000px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2), 0 0 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  transform-style: preserve-3d;
  transition: all 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.25), 0 0 30px rgba(0, 0, 0, 0.15);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.login-form {
  margin-bottom: 24px;
}

.material-input :deep(.el-input__wrapper) {
  background: transparent;
  border: none;
  border-radius: 0;
  border-bottom: 2px solid #dcdfe6;
  box-shadow: none !important;
  transition: all 0.3s;
}

.material-input :deep(.el-input__wrapper):hover,
.material-input :deep(.el-input__wrapper.is-focus) {
  border-bottom-color: #409EFF;
}

.material-input :deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
  padding-left: 0;
}

.material-select {
  width: 100%;
}

.material-select :deep(.el-input__wrapper) {
  background: transparent;
  border: none;
  border-radius: 0;
  border-bottom: 2px solid #dcdfe6;
  box-shadow: none !important;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  margin-top: 24px;
  background: linear-gradient(45deg, #409EFF, #36cfc9);
  border: none;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
      120deg,
      transparent,
      rgba(255, 255, 255, 0.2),
      transparent
  );
  transition: all 0.5s;
}

.login-button:hover::before {
  left: 100%;
}

.login-button.button-loading {
  background: #409EFF;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 16px;
}

.register-link,
.forget-link {
  color: #409EFF;
  font-size: 14px;
  transition: all 0.3s ease;
}

.register-link:hover,
.forget-link:hover {
  color: #66b1ff;
  transform: translateY(-1px);
}

@media (max-width: 768px) {
  .login-content {
    padding: 16px;
  }

  .login-card {
    padding: 24px;
  }

  .login-header h2 {
    font-size: 24px;
  }

  .subtitle {
    font-size: 14px;
  }
}
</style>